﻿using System;
using PTiRouZ.Net.SilverLight.Api;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.IO;

namespace PTiRouZ.Net.SilverLight.Api.HelloTipi
{
    /// <summary>Objet du résultat d'une requête d'upload d'une photo</summary>
    /// <see cref="upload"></see>
    [DataContract]
    public class upload
    {
        /// <summary>statuts de l'envoie de la photo = 'ok'</summary>
        [DataMember]
        public string status { get; set; }

        /// <summary>id de la photo que l'on vient de poster</summary>
        [DataMember]
        public int photo_id { get; set; }

        /// <summary>id de l'album où la photo a été enregistrée</summary>
        [DataMember]
        public int album_id { get; set; }

        /// <summary>url de la miniature de la photo</summary>
        /// <remarks>durée de validité de l'url: 1 heure</remarks>
        [DataMember]
        public string url_small { get; set; }

        /// <summary>url de la version moyenne de la photo</summary>
        /// <remarks>durée de validité de l'url: 1 heure</remarks>
        [DataMember]
        public string url_medium { get; set; }
    }

    public delegate void UploadEventHandler(object sender, UploadEventArgs e);
    public class UploadEventArgs : EventArgs
    {
        public UploadEventArgs(upload u)
        {
            _result = u;
        }

        public UploadEventArgs(upload u, string f)
        {
            _result = u;
            _filename = f;
        }

        private upload _result;
        public upload result { get { return _result; } }

        private string _filename;
        public string filename { get { return _filename; } set { _filename = value; } }
    }

    public partial class apitxt : api
    {

        private string filename;
        
        public event UploadEventHandler UploadCompleted;

        /// <summary>Permet de poster une photo dans un site de famille de l'utilisateur.</summary>
        /// <c>http://apitxt.hellotipi.com/photo/upload</c>
        /// <param name="site">nom du site de famille: obligatoire</param>
        /// <param name="image">chemin du fichier de la photo: obligatoire</param>
        /// <param name="album_id">id de l'album dans lequel publier la photo: factultatif</param>
        /// <returns>Retourne les url de la photo que l'on vient de poster.</returns>
        public void upload(string site, Stream image, String name, int album_id = 0)
        {

            this.filename = name;

            base.APICallCompleted += new APICallCompletedEventHandler(UploadCallCompleted);

            var querystring = new List<KeyValuePair<string, string>>
                             {
                                 new KeyValuePair<string, string>("site", site),
                                 new KeyValuePair<string, string>("image", name),
                             };

            if (album_id > 0)
                querystring.Add(new KeyValuePair<string, string>("album_id", album_id.ToString()));

            PostUrlApi("photo/upload.json", querystring, "image", image);

        }

        void UploadCallCompleted(object sender, ApiOpenReadCompletedEventArgs e)
        {
            try
            {
                UploadEventArgs uea = new UploadEventArgs(serializeTools.Deserialize<upload>(e.Result));

                if (!String.IsNullOrWhiteSpace(this.filename))
                    uea.filename = this.filename;

                UploadCompleted.Invoke(sender, uea);
            }
            finally
            {
                base.APICallCompleted -= new APICallCompletedEventHandler(UploadCallCompleted);
            }
        }
    }
}
